package xyc.szk.sjxy.servlet;/*
 * 作者：zjLiu
 * 日期：2021-05-21
 * 公司：数学与计算机学院
 */

import xyc.szk.sjxy.pojo.Users;

import javax.jws.WebService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet  extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //登录验证逻辑
        //1、获取请求参数
        String userName = req.getParameter("userName");
        String userPwd = req.getParameter("userPwd");
        //封装这两个参数到一个users类的javaBean中
        Users user = new Users();
        user.setUserName(userName);
        user.setUserPwd(userPwd);
        //比对帐号和密码：admin 123
        if (userName.equals("admin")&&userPwd.equals("123")) {
            //登录成功
            //将验证通过的user对象存入到session域中
            req.getSession().setAttribute("loginInfo",user);
            //重定向 页面: /admin/list.jsp
            resp.sendRedirect(req.getContextPath()+"/admin/list.jsp");
        }else{
            //身份验证没有过 重定向页面: /admin/login.jsp
            resp.sendRedirect(req.getContextPath()+"/login.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
